﻿using Motion.ZmcAct;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dir = Motion.ZmcAct.Dir;

namespace WindowsFormsApp1.控件
{
    public partial class MoveStationContorl : UIUserControl
    {
        public MoveStationContorl()
        {
            InitializeComponent();
            InitEvent();
        }

        private void InitEvent()
        {
            this.uiLbYAdd.Click += UiLbYAdd_Click;
            this.uiLbYSub.Click += UiLbYSub_Click;
            this.uiLbXAdd.Click += UiLbXAdd_Click;
            this.uiLbXSub.Click += UiLbXSub_Click;
            this.uiLbZAdd.Click += UiLbZAdd_Click;
            this.uiLbZSub.Click += UiLbZSub_Click;
            this.uiLbRAdd.Click += UiLbRAdd_Click;
            this.uiLbRSub.Click += UiLbRSub_Click;
            this.uiLbXAdd.MouseDown += UiLbXAdd_MouseDown;
            this.uiLbXAdd.MouseUp += UiLbXAdd_MouseUp;
            this.uiLbXSub.MouseDown += UiLbXSub_MouseDown;
            this.uiLbXSub.MouseUp += UiLbXSub_MouseUp;

            this.uiLbYAdd.MouseDown += UiLbYAdd_MouseDown;
            this.uiLbYAdd.MouseUp += UiLbYAdd_MouseUp;
            this.uiLbYSub.MouseDown += UiLbYSub_MouseDown;
            this.uiLbYSub.MouseUp += UiLbYSub_MouseUp;

            this.uiLbZAdd.MouseDown += UiLbZAdd_MouseDown;
            this.uiLbZAdd.MouseUp += UiLbZAdd_MouseUp;
            this.uiLbZSub.MouseDown += UiLbZSub_MouseDown;
            this.uiLbZSub.MouseUp += UiLbZSub_MouseUp;

            this.uiLbRAdd.MouseDown += UiLbRAdd_MouseDown;
            this.uiLbRAdd.MouseUp += UiLbRAdd_MouseUp;
            this.uiLbRSub.MouseUp += UiLbRSub_MouseUp;
            this.uiLbRSub.MouseDown += UiLbRSub_MouseDown;

            uiLbMoveSpeed.Click += UiLbMoveSpeed_Click;
            this.btnMoveMode.Click += btnMoveMode_Click;
        }

        private void UiLbRSub_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexR, Dir.Negative, JogMode.Step, AxisSpeedCur, Dis);
        }

        private void UiLbRAdd_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexR, Dir.Forward, JogMode.Step, AxisSpeedCur, Dis);
        }

        private void UiLbRSub_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexR, Dir.Negative, JogMode.Always, AxisSpeedCur, Dis);
        }

        private void UiLbRSub_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexR);
        }

        private void UiLbRAdd_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexR);
        }

        private void UiLbRAdd_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexR, Dir.Forward, JogMode.Always, AxisSpeedCur, Dis);
        }
        #region 事件


        int AxisIndexX
        {
            get
            {

                return 0;
            }
        }
        int AxisIndexY
        {
            get
            {

                return 1;
            }
        }
        int AxisIndexZ
        {
            get
            {

                return 2;
            }
        }
        int AxisIndexR
        {
            get
            {

                return 3;
            }
        }
        public AxisSpeed AxisSpeedCur { get; set; } = AxisSpeed.Middle;
        public double Dis
        {
            get { return num寸动距离.Value; }
        }
        private void UiLbZSub_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexZ);
        }

        private void UiLbZSub_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexZ, Dir.Negative, JogMode.Always, AxisSpeedCur, Dis);
        }

        private void UiLbZAdd_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexZ);
        }

        private void UiLbZAdd_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexZ, Dir.Forward, JogMode.Always, AxisSpeedCur, Dis);
        }

        private void UiLbZSub_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexZ, Dir.Negative, JogMode.Step, AxisSpeedCur, Dis);
        }

        private void UiLbZAdd_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexZ, Dir.Forward, JogMode.Step, AxisSpeedCur, Dis);
        }
        bool IsJog { get; set; } = true;
        private void UiLbYSub_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexY);
        }
        private void UiLbYSub_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexY, Motion.ZmcAct.Dir.Negative, JogMode.Always, AxisSpeedCur, Dis);
        }
        private void UiLbYAdd_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexY);
        }
        private void UiLbYAdd_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexY, Dir.Forward, JogMode.Always, AxisSpeedCur, Dis);
        }

        private void UiLbXSub_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexX);
        }


        private void UiLbXSub_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexX, Dir.Negative, JogMode.Always, AxisSpeedCur, Dis);
        }

        private void UiLbXAdd_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogStop(AxisIndexX);
        }

        private void UiLbXAdd_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexX, Dir.Forward, JogMode.Always, AxisSpeedCur, Dis);
        }
        private void UiLbXAdd_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }

            ZMC.Instance.JogAxis(AxisIndexX, Dir.Forward, JogMode.Step, AxisSpeedCur, Dis);
        }
        private void UiLbXSub_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexX, Dir.Negative, JogMode.Step, AxisSpeedCur, Dis);
        }
        private void UiLbYAdd_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexY, Dir.Forward, JogMode.Step, AxisSpeedCur, Dis);
        }

        private void UiLbYSub_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                return;
            }
            ZMC.Instance.JogAxis(AxisIndexY, Dir.Negative, JogMode.Step, AxisSpeedCur, Dis);
        }
        private void UiLbMoveSpeed_Click(object sender, EventArgs e)
        {
            uiLbMoveSpeed.Invoke(new Action(() =>
            {
                if (uiLbMoveSpeed.Text == "低速")
                {
                    uiLbMoveSpeed.Text = "中速";
                    uiLbMoveSpeed.BackColor = Color.LimeGreen;
                    AxisSpeedCur = AxisSpeed.Middle;
                }
                else if (uiLbMoveSpeed.Text == "中速")
                {
                    uiLbMoveSpeed.Text = "高速";
                    uiLbMoveSpeed.BackColor = Color.Red;
                    AxisSpeedCur = AxisSpeed.Hight;
                }
                else if (uiLbMoveSpeed.Text == "高速")
                {
                    uiLbMoveSpeed.Text = "低速";
                    uiLbMoveSpeed.BackColor = Color.Yellow;
                    AxisSpeedCur = AxisSpeed.Low;
                }
            }));
        }
        private void btnMoveMode_Click(object sender, EventArgs e)
        {
            if (IsJog)
            {
                IsJog = false;
                btnMoveMode.Text = "寸动";
            }
            else
            {
                IsJog = true;
                btnMoveMode.Text = "连续";
            }
        }
        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            var xPos = ZMC.Instance.GetAxisPos(AxisIndexX);
            var yPos = ZMC.Instance.GetAxisPos(AxisIndexY);
            var zPos = ZMC.Instance.GetAxisPos(AxisIndexZ);
            var RPos = ZMC.Instance.GetAxisPos(AxisIndexR);
            ZMC.Instance.ReadUserVar("ga_MainX1Encoder",out float xPosEncoder);
            ZMC.Instance.ReadUserVar("ga_MainY1Encoder", out float yPosEncoder);
            ZMC.Instance.ReadUserVar("ga_MainZ1Encoder", out float zPosEncoder);
            ZMC.Instance.ReadUserVar("ga_MainR1Encoder", out float RPosEncoder);

            //var yPosEncoder = ZMC.Instance.GetAxisEncoderPos(AxisIndexY);
            //var zPosEncoder = ZMC.Instance.GetAxisEncoderPos(AxisIndexZ);
            //var RPosEncoder = ZMC.Instance.GetAxisEncoderPos(AxisIndexR);

            lblPos.Text = $"命令位置-X:{xPos} Y:{yPos} Z:{zPos} R:{RPos}";
            lblEncoder.Text = $"编码器位置-X:{(xPosEncoder/1000).ToString("0.000")} Y:{(yPosEncoder/1000).ToString("0.000")} Z:{(zPosEncoder/1000).ToString("0.000")} R:{(RPosEncoder/1000).ToString("0.000")}";

        }

        private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBox1.SelectedIndex == -1)
                return;
            int index = uiComboBox1.SelectedIndex;
            ZMC zmc = ZMC.Instance;
            zmc.ReadArrayIndex("ga_MainX1Position", index, out float x);
            zmc.ReadArrayIndex("ga_MainY1Position", index, out float y);
            zmc.ReadArrayIndex("ga_MainZ1Position", index, out float z);
            zmc.ReadArrayIndex("ga_MainR1Position", index, out float r);
            uiDoubleUpDownX.Value = x; 
            uiDoubleUpDownY.Value = y;
            uiDoubleUpDownZ.Value = z;
            uiDoubleUpDownR.Value = r;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            var xPos = ZMC.Instance.GetAxisPos(AxisIndexX);
            var yPos = ZMC.Instance.GetAxisPos(AxisIndexY);
            var zPos = ZMC.Instance.GetAxisPos(AxisIndexZ);
            var RPos = ZMC.Instance.GetAxisPos(AxisIndexR);

            uiDoubleUpDownX.Value = xPos;
            uiDoubleUpDownY.Value = yPos;
            uiDoubleUpDownZ.Value = zPos;
            uiDoubleUpDownR.Value = RPos;

        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (uiComboBox1.SelectedIndex == -1)
                return;
            ZMC zmc = ZMC.Instance;
            int index = uiComboBox1.SelectedIndex;

            zmc.WriteArrayIndex("ga_MainX1Position", index, (float)uiDoubleUpDownX.Value);
            zmc.WriteArrayIndex("ga_MainY1Position", index, (float)uiDoubleUpDownY.Value);
            zmc.WriteArrayIndex("ga_MainZ1Position", index, (float)uiDoubleUpDownZ.Value);
            zmc.WriteArrayIndex("ga_MainR1Position", index, (float)uiDoubleUpDownR.Value);
        }
    }
}
